//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace.c;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import com.baidu.trace.LBSTraceService;
import com.baidu.trace.h;
import com.baidu.trace.api.bos.OnBosListener;
import com.baidu.trace.model.BaseRequest;
import com.baidu.trace.model.ProtocolType;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSession;

public final class a {
    private static final Pattern c = Pattern.compile("[0-9A-Fa-f]{4}");
    private static a.a d = null;
    private static a.b e = null;
    private static WeakReference<Context> f = null;
    private static boolean g = false;
    public static ExecutorService a = null;
    public static ProtocolType b;
    private static int h;
    private static int i;
    private static long j;

    public static synchronized void a(Context var0, Class<?> var1) {
        if(f == null) {
            f = new WeakReference(var0);
        }

        if(!g || a == null) {
            d = new a.a();
            e = new a.b(0);
            if(LBSTraceService.class.getSimpleName().equals(var1.getSimpleName())) {
                a = Executors.newSingleThreadExecutor();
            } else {
                a = new ThreadPoolExecutor(h, i, j, TimeUnit.SECONDS, new LinkedBlockingQueue());
            }

            g = true;
        }
    }

    public static synchronized boolean a(int var0, int var1, long var2) {
        if(var0 >= 0 && var1 > 0 && var1 >= var0 && var2 >= 0L) {
            if(a instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor var4;
                (var4 = (ThreadPoolExecutor)a).setCorePoolSize(var0);
                var4.setMaximumPoolSize(var1);
                var4.setKeepAliveTime(var2, TimeUnit.SECONDS);
            } else {
                h = var0;
                i = var1;
                j = var2;
            }

            return true;
        } else {
            return false;
        }
    }

    public static synchronized void a() {
        if(f != null) {
            f.clear();
            f = null;
        }

        if(g && a != null) {
            try {
                if(!a.isShutdown()) {
                    a.shutdownNow();
                }
            } catch (Exception var0) {
                ;
            }

            d = null;
            e = null;
            a = null;
            g = false;
        }
    }

    public static void a(BaseRequest var0, int var1, String var2, a.c var3, int var4, String var5, Object var6) {
        a.d var7;
        (var7 = new a.d()).a = var1;
        var7.b = var4;
        var7.c = var2;
        var7.d = var3;
        var7.e = var5;
        var7.f = var0;
        var7.g = var6;
        if(a != null && !a.isShutdown()) {
            a.execute(var7);
        }

    }

    private static String b(InputStream var0) {
        if(var0 == null) {
            return "";
        } else {
            BufferedReader var39 = new BufferedReader(new InputStreamReader(var0));
            StringBuffer var1 = new StringBuffer();

            try {
                String var2;
                try {
                    while(null != (var2 = var39.readLine())) {
                        var1.append(var2);
                    }
                } catch (Exception var37) {
                    ;
                }
            } finally {
                if(var39 != null) {
                    try {
                        var39.close();
                    } catch (IOException var35) {
                        ;
                    } finally {
                        ;
                    }
                }

            }

            return var1.toString();
        }
    }

    static {
        b = ProtocolType.HTTPS;
        h = 5;
        i = 2147483647;
        j = 60L;
    }

    static class a extends Handler {
        a() {
        }

        public final void handleMessage(Message var1) {
            super.handleMessage(var1);
        }
    }

    public static enum c {
        a,
        b;

        private c() {
        }

        static {
            a.c[] var10000 = new a.c[]{a, b};
        }
    }

    static class b implements HostnameVerifier {
        private b() {
        }

        public final boolean verify(String var1, SSLSession var2) {
            return "api.map.baidu.com".equals(var1);
        }
    }

    static class d implements Runnable {
        protected int a;
        protected int b;
        protected String c;
        protected a.c d;
        protected String e;
        protected BaseRequest f;
        protected Object g;

        d() {
        }

        public final String toString() {
            return "TaskRunnable [tag=" + this.f.getTag() + ", serviceTag=" + this.a + ", operateType=" + this.b + ", action=" + this.c + ", requestType=" + this.d + ", parameters=" + this.e + "]";
        }

        public final void run() {
            if(this.f.isCanceled()) {
                com.baidu.trace.a.a("request has been canceled!");
            } else if(null != a.f && null != a.f.get() && !com.baidu.trace.b.d.a((Context)a.f.get())) {
                a.d.post(new com.baidu.trace.c.b(this));
            } else {
                StringBuffer var1 = new StringBuffer();

                try {
                    if(ProtocolType.HTTPS != a.b && 3 != this.a) {
                        if(ProtocolType.HTTP == a.b) {
                            var1.append(this.a(ProtocolType.HTTP, "http://api.map.baidu.com/sdkproxy/v2/lbs_tracesdk/trace/v3/" + this.c));
                        }
                    } else {
                        var1.append(this.a(ProtocolType.HTTPS, "https://api.map.baidu.com/sdkproxy/v2/lbs_tracesdk/trace/v3/" + this.c));
                    }
                } catch (Exception var6) {
                    com.baidu.trace.a.a("http request failed");
                }

                if(this.f.isCanceled()) {
                    com.baidu.trace.a.a("request has been canceled!");
                } else if(!TextUtils.isEmpty(var1) && 3 == this.a && 3 != this.b) {
                    BaseRequest var10000 = this.f;
                    String var10001 = var1.toString();
                    int var10002 = this.b;
                    OnBosListener var10003 = (OnBosListener)this.g;
                    a.a var5 = a.d;
                    OnBosListener var4 = var10003;
                    int var3 = var10002;
                    String var2 = var10001;
                    h.a(var10000, var3, true, 0, a.a(var2), var4, var5);
                } else {
                    a.d.post(new com.baidu.trace.c.c(this, var1));
                }
            }
        }

        private String a(ProtocolType var1, String var2) throws Exception {
            URL var4 = this.a(var2);
            if(var4 == null) {
                return "";
            } else {
                Object var3;
                if(ProtocolType.HTTPS == var1) {
                    var3 = (HttpsURLConnection)var4.openConnection();
                } else {
                    var3 = (HttpURLConnection)var4.openConnection();
                }

                return this.a((HttpURLConnection)var3)?this.b((HttpURLConnection)var3):"";
            }
        }

        private URL a(String var1) {
            URL var2 = null;

            try {
                if(a.c.a == this.d) {
                    var2 = new URL(var1 + "?" + this.e);
                } else if(a.c.b == this.d) {
                    var2 = new URL(var1);
                }

                return var2;
            } catch (MalformedURLException var3) {
                return null;
            }
        }

        private boolean a(HttpURLConnection var1) {
            try {
                var1.setRequestMethod(this.d.name());
                var1.setConnectTimeout(10000);
                var1.setReadTimeout(15000);
                var1.setUseCaches(false);
                return true;
            } catch (ProtocolException var2) {
                return false;
            }
        }

        private String b(HttpURLConnection var1) {
            InputStream var2 = null;
            boolean var59 = false;

            String var3;
            label587: {
                label588: {
                    try {
                        var59 = true;
                        OutputStream var71;
                        BufferedWriter var72;
                        int var73;
                        if(var1 instanceof HttpsURLConnection) {
                            ((HttpsURLConnection)var1).setHostnameVerifier(a.e);
                            if(a.c.b == this.d) {
                                var1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                                var1.setDoOutput(true);
                                var1.setDoInput(true);
                                var71 = var1.getOutputStream();
                                (var72 = new BufferedWriter(new OutputStreamWriter(var71, "UTF-8"))).write(this.e);
                                var72.flush();
                                var72.close();
                                var1.connect();
                                var2 = var1.getInputStream();
                            } else if(a.c.a == this.d) {
                                var1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                                var73 = var1.getResponseCode();
                                if(200 == var73) {
                                    var2 = var1.getInputStream();
                                }
                            }
                        } else if(var1 instanceof HttpURLConnection) {
                            if(a.c.a == this.d) {
                                var73 = var1.getResponseCode();
                                if(200 == var73) {
                                    var2 = var1.getInputStream();
                                }
                            } else if(a.c.b == this.d) {
                                var1.setDoOutput(true);
                                var1.setDoInput(true);
                                var1.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                                var71 = var1.getOutputStream();
                                (var72 = new BufferedWriter(new OutputStreamWriter(var71, "UTF-8"))).write(this.e);
                                var72.flush();
                                var72.close();
                                var1.connect();
                                var2 = var1.getInputStream();
                            }
                        }

                        var3 = a.b(var2);
                        var59 = false;
                        break label587;
                    } catch (SSLHandshakeException var68) {
                        com.baidu.trace.a.a("BaiduTraceSDK", "current network is unavailable or unstable");
                        var3 = "";
                        var59 = false;
                    } catch (Exception var69) {
                        var59 = false;
                        break label588;
                    } finally {
                        if(var59) {
                            if(var2 != null) {
                                try {
                                    var2.close();
                                } catch (IOException var60) {
                                    ;
                                } finally {
                                    ;
                                }
                            }

                            if(var1 != null) {
                                var1.disconnect();
                            }

                        }
                    }

                    if(var2 != null) {
                        try {
                            var2.close();
                        } catch (IOException var64) {
                            ;
                        } finally {
                            ;
                        }
                    }

                    if(var1 != null) {
                        var1.disconnect();
                    }

                    return var3;
                }

                var3 = "";
                if(var2 != null) {
                    try {
                        var2.close();
                    } catch (IOException var62) {
                        ;
                    } finally {
                        ;
                    }
                }

                if(var1 != null) {
                    var1.disconnect();
                }

                return var3;
            }

            if(var2 != null) {
                try {
                    var2.close();
                } catch (IOException var66) {
                    ;
                } finally {
                    ;
                }
            }

            if(var1 != null) {
                var1.disconnect();
            }

            return var3;
        }
    }
}
